/*
 * @(#)WebModule.java $version 2010. 8. 11
 *
 * Copyright 2007-2010 NHN Corp. All rights Reserved. 
 * NHN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
 */
package com.nhncorp.usf.core.jmx;

import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

import javax.management.NotCompliantMBeanException;
import javax.management.StandardMBean;

/**
 * @author Web Platform Development Team
 */
public class WebModule extends StandardMBean implements WebModuleMBean {
    String name;
    String serverName;
    private Map<String, Long> execTimeMap = new ConcurrentHashMap<String, Long>();
    private Map<String, Integer> reqCountMap = new ConcurrentHashMap<String, Integer>();
    @SuppressWarnings("unchecked")
	private Map<String, Map> resultCodeCountMap = new ConcurrentHashMap<String, Map>();

    /**
     * Instantiates a new web module.
     *
     * @param name    the context name
     * @param mbeanInterface the mbean interface
     * @throws NotCompliantMBeanException the not compliant m bean exception
     */
    public WebModule(String name, Class<?> mbeanInterface) throws NotCompliantMBeanException {
        super(WebModuleMBean.class);
        this.name = name;
    }

    /**
     *
     * @return the context name
     */
    public String getName() {
        return name;
    }

    /**
     *
     * @return the total web module statistic data
     */
    @SuppressWarnings("unchecked")
	public Map stat() {
        return getTotalWebModuleStatus();
    }

    /**
     * Gets the total web module status.
     *
     * @return the total web module status
     */
    @SuppressWarnings("unchecked")
	public Map getTotalWebModuleStatus() {
        Map<String, String> result = new HashMap<String, String>();

        for (String key : execTimeMap.keySet()) {
            result.put(REQTIME_ITEM_PREFIX + SEPERATOR_TYPE + key + SEPERATOR_TYPE + STATUS_TYPE,
                       String.valueOf(execTimeMap.get(key)));
        }

        for (String key : reqCountMap.keySet()) {
            result.put(REQCOUNT_ITEM_PREFIX + SEPERATOR_TYPE + key + SEPERATOR_TYPE + STATUS_TYPE,
                       String.valueOf(reqCountMap.get(key)));
        }

        for (String key : resultCodeCountMap.keySet()) {
            Map returnCodeMap = resultCodeCountMap.get(key);

            for (Object errorCode : returnCodeMap.keySet()) {
                result.put(ERRORCODE_ITEM_PREFIX + errorCode + SEPERATOR_TYPE + key + SEPERATOR_TYPE + STATUS_TYPE,
                           String.valueOf(returnCodeMap.get(errorCode)));
            }

        }

        return result;
    }

    /**
     *
     * @param uri the http request uri
     * @param value the monitoring data
     */
    public void setReqTime(String uri, long value) {
        Long oldValue = execTimeMap.get(uri);
        long newValue = value;

        if (oldValue != null) {
            newValue += oldValue; // total exectime 수행 시간
        }

        execTimeMap.put(uri, newValue);
    }

    /**
     *
     * @param uri the http request uri
     */
    public void setReqCount(String uri) {
        Integer oldValue = reqCountMap.get(uri);

        if (oldValue == null) {
            reqCountMap.put(uri, 1);
        } else {
            reqCountMap.put(uri, oldValue + 1);
        }

    }

    /**
     *
     * @param uri the http request uri
     * @param key the monitoring key
     */
    @SuppressWarnings("unchecked")
	public void putResultCodeCount(String uri, String key) {
        Map oldValue = resultCodeCountMap.get(uri);

        if (oldValue == null) {
            oldValue = new ConcurrentHashMap<String, Integer>();
            oldValue.put(key, 1);
        } else {
            Integer oldCount = (Integer)oldValue.get(key);

            if (oldCount == null) {
                oldValue.put(key, 1);
            } else {
                oldValue.put(key, ++oldCount);
            }

        }

        resultCodeCountMap.put(uri, oldValue);
    }

    /**
     * context statistic data clear
     */
    public void clear() {
        execTimeMap.clear();
        reqCountMap.clear();
        resultCodeCountMap.clear();
    }
}
